package com.hhxy.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author ghp
 * @date 2023/4/23
 * @title
 * @description
 */
@Configuration
// 开启Swagger和Knife4j
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {

    /**
     * 引入Knife4j提供的扩展类，用于自定义文档
     */
    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public SwaggerConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    /**
     * 创建API文档
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        String groupName = "hhxy";
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName(groupName) // 分组名称
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hhxy.controller"))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(groupName)); // 赋予插件体系
    }

    /**
     * API文档相关信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        Contact contact = new Contact("ghp", "https://blog.csdn.net/qq_66345100?type=blog", "3254213612@qq.com");
        return new ApiInfoBuilder()
                .title("个人博客") // 文档标题
                .description("个人博客接口文档") // 文档描述信息
                .version("1.0").contact(contact) // 文档版本及文档作者信息
                .termsOfServiceUrl("127.0.0.1") // 服务的IP
                .build();
    }
}
